package modes.singletons;

/**
 * 静态内部类加载方式加载单例
 *
 * @author longzj
 */
public class LazyWithInnerClass {

    public static void main(String[] args) {
        final int count = 10;
        for (int i = 0; i < count; i++) {
            new TestThread(String.valueOf(i)).start();
        }
    }

    private static LazyWithInnerClass getInstance() {
        return ClassHolder.LAZY_SINGLETON_WITH_SYNC;
    }

    private static class ClassHolder {
        final static LazyWithInnerClass LAZY_SINGLETON_WITH_SYNC = new LazyWithInnerClass();
    }

    private static class TestThread extends Thread {

        LazyWithInnerClass mSingleton = getInstance();

        TestThread(String name) {
            super("TestThread " + name);
        }

        @Override
        public void run() {
            System.out.println(getInstance().toString());
        }
    }
}
